package com.bank.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bank.pojo.BankMgrLog;
import com.bank.vo.BankLogResult;

@Component("mgrLogDao")
@Transactional
public class MgrLogDao extends BaseDao{
	public boolean addLog(BankMgrLog log){
		getSession().save(log);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public List<BankLogResult> getLogByPage(int pageNum){
		List<BankLogResult> list=new ArrayList<BankLogResult>();
		list=getSession().createQuery("select new com.bank.vo.BankLogResult(b.mgrId,b.bankAdmin.adminId,b.bankAdmin.adminLoginname,b.mgrObj, "
					+ "b.mgrType,b.mgrResult,b.mgrDate) from BankMgrLog b order by b.mgrDate asc").setMaxResults(5)
					.setFirstResult((pageNum-1)*5).list();
		list.get(0).setNum(getLogNum());
		list.get(0).setPages(getLogPage(getLogNum()));
		return list;
	}
	
	public int getLogNum(){
		return getSession().createQuery("from BankMgrLog").list().size();
	}
	
	public int getLogPage(int size){
		if(size%5==0){
			return size/5;
		}
		return size/5+1;
	}
}
